<?php 	namespace Admin\Controller;
class GoodsController extends CommonController
{
	protected $db;
	public function __init()
	{
		$this->db = new \Common\Model\Goods;
	}
	public function index()
	{
		$data = $this->db->get();
		
		View::with("data",$data);
		View::make();
	}
	public function add()
	{
		$category = Db::table("category")->get();
//		p($category);die;
		$category = Data::tree($category,"category_name","category_id","category_pid");
//		p($category);die;

		View::with("category",$category);
		$brand = Db::table("brand")->get();
		View::with("brand",$brand);
		if(IS_POST)
		{
//			p($_POST);die;
			if(!$this->db->store()){
				View::error($this->db->getError());
			}else{
				View::success("添加成功");
			}
			
			
				
		}
		View::make('addGoods');
	}
	public function ajaxGetAttr()
	{
		if(IS_AJAX)
		{
			$type_id = $_POST['type_id'];
			//在类型属性表中，根据post提交的类型id获取数据
			//type_ID,是类型属性表当中的类型id
			$typeAttrData = Db::table("type_attr")->where("tyattr_type_id",$type_id)->get();
//			p($typeAttrData);die;
			//把属性值转为数组
			foreach ($typeAttrData as $k=>$v) {
				$typeAttrData[$k]['tyattr_value']=explode("|", $v['tyattr_value']);
			}
			//View::ajax($typeAttrData);
			echo json_encode($typeAttrData);
		}
	}
	public function ajaxUpload(){
		$files = Upload::path("Upload/goods/" . date('ymd'))->make();
		if($files)
		{
			View::ajax($files[0]);
		}
		View::ajax("上传错误");
	}
	public function delImg(){
		if(IS_AJAX){
			$path = $_POST['path'];
			if(unlink($path)){
				View::ajax(1);
			}
			View::ajax(0);
		}
	}
	public function edit()
	{
		View::make();
	}
}
